﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.BaseInfoServiceRef;

    public class RentWhDetail : BasePage
    {
        protected Button btnCancel;
        protected Button btnSave;
        protected HtmlForm F9013;
        protected HtmlInputHidden hdnContractID;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnType;
        protected HtmlHead Head1;
        protected Label lblArea;
        protected Label lblLineID;
        protected Label lblLocation;
        protected Label lblManageAmount;
        protected Label lblMsg;
        protected Label lblRemark;
        protected Label lblRentWhDetail;
        protected Label lblTotalPrice;
        protected Label lblUnitPrice;
        protected Label lblZoneNO;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtArea;
        protected TextBox txtLineID;
        protected TextBox txtLocation;
        protected TextBox txtManageAmount;
        protected TextBox txtRemark;
        protected TextBox txtTotalPrice;
        protected TextBox txtUnitPrice;
        protected TextBox txtZoneNO;

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Contract.aspx?from={0}&detail={1}&ID={2}&detail=detail", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.hdnContractID.Value.Trim())));
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtLocation.Text.Trim()))
            {
                this.lblMsg.Text = "位置不允许为空!";
                this.txtLocation.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtArea.Text.Trim()))
            {
                this.lblMsg.Text = "面积不允许为空!";
                this.txtArea.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtUnitPrice.Text.Trim()))
            {
                this.lblMsg.Text = "单价不允许为空!";
                this.txtUnitPrice.Focus();
            }
            else
            {
                bool success = false;
                try
                {
                    WMS6.Web.BaseInfoDetailServiceRef.RentWhDetailInfo rentWhDetailInfo = null;
                    if (base.DataEntityViewState != null)
                    {
                        rentWhDetailInfo = base.DataEntityViewState as WMS6.Web.BaseInfoDetailServiceRef.RentWhDetailInfo;
                    }
                    else
                    {
                        rentWhDetailInfo = new WMS6.Web.BaseInfoDetailServiceRef.RentWhDetailInfo();
                    }
                    rentWhDetailInfo.ContractID = this.hdnContractID.Value;
                    rentWhDetailInfo.LineID = int.Parse(this.txtLineID.Text.Trim());
                    rentWhDetailInfo.UnitPrice = decimal.Parse(this.txtUnitPrice.Text);
                    if (!string.IsNullOrEmpty(this.txtTotalPrice.Text))
                    {
                        rentWhDetailInfo.TotalPrice = decimal.Parse(this.txtTotalPrice.Text);
                    }
                    else
                    {
                        rentWhDetailInfo.TotalPrice = 0M;
                    }
                    rentWhDetailInfo.Location = this.txtLocation.Text;
                    rentWhDetailInfo.Area = decimal.Parse(this.txtArea.Text);
                    rentWhDetailInfo.Remark = this.txtRemark.Text;
                    rentWhDetailInfo.ZoneNO = this.txtZoneNO.Text.Trim();
                    decimal tempDec = 0M;
                    if (!string.IsNullOrEmpty(this.txtManageAmount.Text.Trim()))
                    {
                        if (decimal.TryParse(this.txtManageAmount.Text.Trim(), out tempDec))
                        {
                            rentWhDetailInfo.ManageAmount = decimal.Parse(this.txtManageAmount.Text.Trim());
                        }
                        else
                        {
                            rentWhDetailInfo.ManageAmount = tempDec;
                        }
                    }
                    else
                    {
                        rentWhDetailInfo.ManageAmount = tempDec;
                    }
                    if (!string.IsNullOrEmpty(this.hdnType.Value))
                    {
                        rentWhDetailInfo.UpdatedBy = base.UserInfo.UserID;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().UpdateDetail(rentWhDetailInfo, "RENT_WH_DETAIL")))
                        {
                            success = true;
                        }
                    }
                    else
                    {
                        rentWhDetailInfo.CreatedBy = base.UserInfo.UserID;
                        rentWhDetailInfo.UpdatedBy = rentWhDetailInfo.CreatedBy;
                        if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(rentWhDetailInfo, "RENT_WH_DETAIL")))
                        {
                            success = true;
                        }
                    }
                    if (success)
                    {
                        base.Response.Redirect(string.Format("Contract.aspx?from={0}&detail={1}&ID={2}&detail=detail", base.UrlEncode(this.hdnFrom.Value), "1", base.UrlEncode(this.hdnContractID.Value.Trim())), false);
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                string from = base.Request.QueryString["from"];
                this.hdnFrom.Value = from;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnContractID.Value = base.Request.QueryString["ID"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["LineID"]))
            {
                this.txtLineID.Text = base.Request.QueryString["LineID"];
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["type"]))
            {
                this.hdnType.Value = base.Request.QueryString["type"];
                this.SetTextBoxByRentWhDetailInfo();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetTextBoxByRentWhDetailInfo()
        {
            try
            {
                ContractInfo contractInfo = (ContractInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(this.hdnContractID.Value, "CONTRACT");
                if ((contractInfo != null) && (contractInfo.IsAudit == "Y"))
                {
                    this.btnSave.Enabled = false;
                }
                WMS6.Web.BaseInfoDetailServiceRef.RentWhDetailInfo rentWhDetailInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnContractID.Value, this.txtLineID.Text, "", "RENT_WH_DETAIL") as WMS6.Web.BaseInfoDetailServiceRef.RentWhDetailInfo;
                if (rentWhDetailInfo != null)
                {
                    this.txtArea.Text = base.GetPreferenceDigit(rentWhDetailInfo.Area);
                    this.txtLocation.Text = rentWhDetailInfo.Location;
                    this.txtRemark.Text = rentWhDetailInfo.Remark;
                    this.txtTotalPrice.Text = (rentWhDetailInfo.TotalPrice > 0M) ? base.GetPreferenceDigit(rentWhDetailInfo.TotalPrice) : "0";
                    this.txtUnitPrice.Text = base.GetPreferenceDigit(rentWhDetailInfo.UnitPrice);
                    this.txtZoneNO.Text = rentWhDetailInfo.ZoneNO;
                    this.txtManageAmount.Text = base.GetPreferenceDigit(rentWhDetailInfo.ManageAmount);
                    base.DataEntityViewState = rentWhDetailInfo;
                }
                else
                {
                    this.lblMsg.Text = this.hdnContractID.Value + base.GetGlobalMsgResource("F4100_021");
                    this.lblMsg.Visible = true;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
                this.lblMsg.Visible = true;
            }
        }
    }
}

